package com.ywgt.system.controller;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.Page;
import com.ywgt.response.Result;
import com.ywgt.system.pojo.Department;
import com.ywgt.system.service.DepartmentService;
import com.ywgt.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yiping
 * @since 2021-01-15
 */
@RestController
@RequestMapping("/system/department")
@Api(value = "部门管理" ,tags = "部门管理")
public class DepartmentController {

    @Autowired
    UserService userService;
    @Autowired
    private DepartmentService departmentService;


    @ApiOperation(value = "获取所有部门",notes = "获取部门列表")
    @GetMapping("/findAll")
    public Result findAll(){
        List<Department> departments = departmentService.list();
        return Result.ok().data("depts",departments);
    }

    @ApiOperation(value = "根据条件获取部门",notes = "根据条件查询部门列表")
    @GetMapping("/findDepartmentList")
    public Result findDepartmentList(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                     @RequestParam(value = "pageSize",defaultValue = "8") Integer pageSize,
    Department department){
        Page<Department> departmentList = departmentService.findDepartmentList(pageNum, pageSize, department);
        if (departmentList.size()>0) {
            long total = departmentList.getTotal();
            List<Department> departments = departmentList.getResult();
            return Result.ok().data("rows", departments).data("total", total);
        }
        return Result.error();
    }

    @ApiOperation(value = "添加部门",notes = "添加部门")
    @PostMapping("/add")
    public Result addDepartment(@RequestBody Department department){
        if (departmentService.save(department)){
            return Result.ok();
        }
        return Result.error();
    }
    @ApiOperation(value = "删除部门",notes = "根据id删除部门")
    @DeleteMapping("/delete/{id}")
    public Result deleteDept(@PathVariable("id")Long id){
        if (departmentService.removeById(id)){
            return Result.ok();
        }
        return Result.error();
    }
    @ApiOperation(value = "部门信息",notes = "根据id获得部门信息")
    @GetMapping("/edit/{id}")
    public Result editDept(@PathVariable("id")Long id){
        Department department = departmentService.getById(id);
        return Result.ok().data("data",department);
    }
    @ApiOperation(value = "更新部门",notes ="更新部门信息" )
    @PutMapping("/update/{id}")
    public Result update(@PathVariable("id") Long id,@RequestBody Department department){
        LambdaUpdateWrapper<Department> updateWrapper=new LambdaUpdateWrapper<>();
        updateWrapper.eq(Department::getId,id);
        if (departmentService.update(department,updateWrapper)){
            return Result.ok();
        }
        return Result.error();
    }
}

